import pygame, math, sys, random 
pygame.init()
joynum = pygame.joystick.get_count()
class Joystickcontrol():
    if joynum >=1:
        joystick = True
        joy1 = pygame.joystick.Joystick(0)
        joy1.init()
    else:
        joystick = False 
    
    
    
    if screenOn == "Game":
                        if event.type == pygame.KEYDOWN:
                            if event.key == pygame.k_r:
                                player.life -= 1
                            elif event.key == pygame.K_RIGHT:
                                player.moving = 'right'
                            elif event.key == pygame.K_LEFT:
                                player.moving = 'left' 
                            elif event.key == pygame.K_LSHIFT:
                                player.Walkspeed = 7 
                            elif event.key == pygame.K_SPACE:
                                if player.fired < Shot_Limit:
                                    self.player.shooting = True
                            elif event.key == pygame.K_BACKQUOTE:
                                Debug = True 
                            elif event.key == pygame.K_ESCAPE:
                                options = True 
                                option()
                            elif event.key == pygame.K_m:
                                if music == 'on':
                                    music = 'off'
                                    pygame.mixer.pause()
                                elif music == 'off':
                                    music  == 'on' 
                                    pygame.mixer.unpause()
                            if debug:
                                if event.key in range(48, 58):
                                    timesplayerDead = 0
                                    lnum = event.key - 48
                                    unloadLevel()
                                    loadLevel('Resources/levels/level' + str(lnum)                                +'.lvl')
                                elif event.key in range(97, 122):
                                    timesPlayerDead = 0
                                    lnum = event.key - 87
                                    unloaderLevel()
                                    loadLevel('Resourses/levels/level' + str(lnum)                                +'.lvl')
                                    
                            elif evenjt.type == pygame.KEYUP:
                                if event.key == pygame.K_RALT:
                                    ALTFLAG = False
                            elif event.key == pygame.K_LALT:
                                ALTFLAG = False 
                            elif event.key == pygame.K_RIGHT:
                                player.moving = ''
                            elif event.key == pygame.K_LEFT:
                                player.moving = ''
                            elif event.key == pygame.K_UP:
                                player.moving = 'down' 
                            elif event.key == pygame.K_LSHIFT:
                                player.walkSpeed = 5
                            elif event.key == pygame.K_BACKQUOTE:
                                debug = False 
                                
                        elif event.type == pygame.JOYBUTTONDOWN:
                            if event.button == 0: #-- 'A' Button 
                                player.jumping = 'up' 
                            elif event.button == 1: #-- 'B' Button 
                                if elf.player.fired < Shot_Limit:
                                    player.shooting = True 
                            elif event.button == 6: #-- 'select' / 'Back' Button 
                                player.life -= 1
                            elif event.button == 3: #-- 'X' Button
                                self.player.WalkingSpeed = 7 
                            elif event.button == 7: #-- 'start' Button
                                    options = True 
                                    Option()
                                    
                        elif event.type == pygame.JOYHATMOTION:
                                    if event.value == (0, 1):
                                        if fullscreen == 0:
                                            fullscreen = pygame.FULLSCREEN
                                        else:
                                            fullscreen = 0
                                        screen = pygame.display.set_mode(size, fullscreen)
                                        screen.blit(background, (0, 0)) 
                                        pygame.display.flip()
                                        
                        elif event.type == pygame.JOYAXISMOTION:
                            #print event.joy, event.axis, event.value
                            if event.axis == 0: #x on left stick
                                if event.value >= .2:
                                    player.moving = 'right'
                                elif event.value <= -.2:
                                    player.moving = 'left'
                                else:
                                    player.moving = ''
                            elif event.axis == 2:
                                    if event.value >= .2 or event.value <= -.2:
                                        player.walkSpeed = 7
                                    else:
                                        player.walkSpeed = 5